﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using InTheHand.Bluetooth;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Storage.Streams;

namespace WpfBluetooth.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class BluetoothMode : ObservableObject
    {
        /// <summary>
        /// 蓝牙信息
        /// </summary>
        public BluetoothDeviceInfo bluetoothDeviceInfo { get; set; }
        /// <summary>
        /// 蓝牙客户端
        /// </summary>
        public BluetoothClient client { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        private string _name;
        /// <summary>
        /// 名称
        /// </summary>
        public string name { get => _name; set => SetProperty(ref _name, value); }

        /// <summary>
        /// 连接
        /// </summary>
        public ICommand Link { get; set; }
        /// <summary>
        /// 配队
        /// </summary>
        public ICommand pair { get; set; }

      
        public BluetoothMode()
        {
            Link = new AsyncRelayCommand(LinkMethod);
            pair = new RelayCommand(pairMethod);
      

        }

       


    

        /// <summary>
        /// 配队
        /// </summary>
        private void pairMethod()
        {
            if (!bluetoothDeviceInfo.Authenticated)
            {
                var dst = BluetoothSecurity.PairRequest(bluetoothDeviceInfo.DeviceAddress);
                if (dst)
                {
                    Console.WriteLine("配对成功");
                }
                else
                {

                    Console.WriteLine("配对失败");
                }
            }
        }

        private async Task LinkMethod()
        {

            var dst = await bluetoothDeviceInfo.GetRfcommServicesAsync(false);

            foreach (var item in dst)
            {
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var client = new BluetoothClient();
                        var endpoint = new BluetoothEndPoint(bluetoothDeviceInfo.DeviceAddress, item);
                        client.Connect(endpoint);

                        var steam = client.GetStream();
                        if (steam.CanRead)
                        {
                            var lenght = steam.ReadByte();
                            if (lenght > 0)
                            {
                                byte[] bytevalue = new byte[steam.ReadByte()];
                                steam.Read(bytevalue);
                                Console.WriteLine($"{item}-{Encoding.UTF8.GetString(bytevalue)}");
                            }
                            else
                            {
                                Console.WriteLine($"{item}-无数据");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"不能读取{item}");
                        }
                        client.Close();
                    }
                    catch (Exception ex) { Console.WriteLine($"{item}-{ex.ToString()}"); }
                });
                //client.Client.Close();
                //steam.Close();
            }
        }
    }
}
